package com.lantosec.hmrp.core.ureport.store;

import com.bstek.ureport.exception.ReportException;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.lantosec.hmrp.modules.system.entity.SysUreport;
import com.lantosec.hmrp.modules.system.service.ISysUreportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class Other implements ReportProvider {
    private String prefix = "3:";
    private String suffix = ".ureport.xml";
    private int type = 9;
    private boolean disabled = false;

    @Autowired
    private ISysUreportService ureportService;

    private String getName(String file) {
        String name = null;
        if (file.startsWith(prefix)) {
            name = file.substring(prefix.length());
        }
        name = name.replaceAll(suffix, "");
        return name;
    }

    private SysUreport getUreport(String file) {
        String name = getName(file);
        SysUreport ureport = ureportService.getById(name);
        if (ureport == null) {
            ureport = ureportService.getByName(name);
        }
        return ureport;
    }

    @Override
    public InputStream loadReport(String file) {
        String name = getName(file);
        SysUreport ureport = ureportService.queryUreport(type, name);
        if (ureport != null) {
            try {
                return new ByteArrayInputStream(ureport.getUreportContent().getBytes());
            } catch (Exception e) {
                throw new ReportException(e);
            }
        }
        throw new ReportException("找不到：" + file);
    }

    @Override
    public void deleteReport(String file) {
        String name = getName(file);
        SysUreport ureport = ureportService.queryUreport(type, name);
        ureportService.removeById(ureport.getUreportId());
    }

    @Override
    public List<ReportFile> getReportFiles() {
        List<SysUreport> ureports = ureportService.listForType(type);
        List<ReportFile> list = new ArrayList<ReportFile>();
        for (SysUreport ureport : ureports) {
            list.add(new ReportFile(ureport.getUreportName(), ureport.getUpdateDate()));
        }
        return list;
    }

    @Override
    public void saveReport(String file, String content) {
        String name = getName(file);
        SysUreport ureport = ureportService.queryUreport(type, name);
        if (ureport != null) {
            ureport.setUpdateDate(new Date());
            ureport.setUreportContent(content);
            ureportService.updateById(ureport);
        } else {
            ureport = new SysUreport();
            ureport.setUreportName(name);
            ureport.setUreportType(type);
            ureport.setUpdateDate(new Date());
            ureport.setUreportContent(content);
            ureportService.save(ureport);
        }
    }

    @Override
    public boolean disabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    @Override
    public String getPrefix() {
        return prefix;
    }

    @Override
    public String getName() {
        return "其他";
    }
}
